'''
Created on Oct 11, 2012

@author: daeheehan
'''
from pprint import pprint
from github3 import login, GitHub
from getpass import getpass, getuser

import datetime
import dateutil.parser
import time
from pprint import pprint
import sys

# Github User information
GITHUB_USER_ID = "booknara"
GITHUB_USER_ID_1 = "booknaratest"
GITHUB_USER_ID_2 = "jangho87"
GITHUB_USER_ID_3 = "luispigu"

GITHUB_USER_PW = "qwer1234"
GITHUB_USER_PW_1 = 'wasd1234'

if __name__ == '__main__':
    pass

gh = login(GITHUB_USER_ID_3, password=GITHUB_USER_PW_1)
print gh.ratelimit_remaining, 'request remaining'

# Github oh-my-zsh Repository information
GITHUB_USER_NAME = 'robbyrussell'
GITHUB_REPOSITORY_NAME = 'oh-my-zsh'

# oh-my-zsh contributors information file
CONTRIBUTORS_FILE = 'oh_my_zsh_contributors.txt'

# oh-my-zsh star information file
CONTRIBUTORS_STAR_FILE = 'oh_my_zsh_contributors_star.txt'

# oh-my-zsh watch information file
CONTRIBUTORS_WATCH_FILE = 'oh_my_zsh_contributors_watch.txt'

project_contributors = []
contributors_star_dict = {}
contributors_watch_dict = {}

try:
    # Github oh-my-zsh project contributors list from files
    f_collaborators = open(CONTRIBUTORS_FILE, 'r')
    
    for line in f_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_contributors.append(user_login)

    f_collaborators.close()
    
    print 'loading contributors file is finished'
    repos = gh.repository(GITHUB_USER_NAME, GITHUB_REPOSITORY_NAME)
#    print repos
#    print '--------------'

    for item in project_contributors:
        print 'login id: ' +  item

        is_star = 0
        user = gh.user(item)

        for star in user.iter_starred():
            if star.id == repos.id:
                is_star = 1
        
#        print is_star
#        contributors_star_dict = {item, is_star}
        contributors_star_dict[item] = is_star

        is_watch = 0
        for watch in user.iter_subscriptions():
#            print watch
            if watch.id == repos.id:
                is_watch = 1
        
#        print is_watch
#        contributors_watch_dict = {item, is_watch}
        contributors_watch_dict[item] = is_watch
        
        if(gh.ratelimit_remaining < 100):
            time.sleep(60 * 60)
#        break

    f_contributors_star = open(CONTRIBUTORS_STAR_FILE, 'w')
    print 'Printing dictionary: star'
    for key, value in contributors_star_dict.items():
        sys.stdout.write(key + ", " + str(value) + '\n')
        f_contributors_star.write(key + ", " + str(value) + '\n')

    f_contributors_star.close()

    f_contributors_watch = open(CONTRIBUTORS_WATCH_FILE, 'w')
    print 'Printing dictionary: watch'
    for key, value in contributors_watch_dict.items():
        sys.stdout.write(key + ", " + str(value) + '\n')
        f_contributors_watch.write(key + ", " + str(value) + '\n')

    f_contributors_watch.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])

print str(gh.ratelimit_remaining) + ' request remaining'

print
print 'Finally the end'